//
//  EditViewController.swift
//  SpinCut
//
//  Created by dai simon on 2024/7/6.
//

import UIKit

class EditViewController: BaseViewController {
    lazy var topBar: UIView = {
        let view = UIView()
        view.backgroundColor = .black
        return view
    }()
    
    lazy var bgView: UIImageView = {
        let view = UIImageView()
        view.image = UIImage(named: "bg_edit")
        return view
    }()
    
    lazy var backBtn: UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "icon_back"), for: .normal)
        btn.addTarget(self, action:#selector(self.actionBack), for: .touchUpInside)
        return btn
    }()
    
    lazy var musicBtn: UIButton = {
        let view = UIButton()
        view.setImage(UIImage(named: "icon_nomusic"), for: .normal)
        view.setImage(UIImage(named: "icon_music"), for: .selected)
        
        view.titleLabel?.font = UIFont.systemFont(ofSize: 12)
        view.setTitleColor(.white, for: .normal)
        return view
    }()
    
    lazy var viewBtn: UIButton = {
       let view = UIButton()
        view.setImage(UIImage(named: "icon_video"), for: .normal)
        view.setTitle("查看教程", for: .normal)
        view.titleLabel?.font = UIFont.systemFont(ofSize: 12)
        view.setTitleColor(.black, for: .normal)
        view.backgroundColor = UIColor(hex: "#FFC300")
        view.clipsToBounds = true
        view.layer.cornerRadius = 15
        return view
    }()
    
    lazy var canvasView: CanvasView = {
        let view = CanvasView()
        return view
    }()
    
    lazy var toolView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor(hex: "#FFFFFF", alpha: 0.1)
        view.clipsToBounds = true
        view.layer.cornerRadius = 10
        return view
    }()
    
    lazy var colorBtn: UIButton = {
        let view = UIButton()
        view.layer.cornerRadius = 10
        view.backgroundColor = .red
        return view
    }()
    
    lazy var penBtn: UIButton = {
        let view = UIButton()
        view.setImage(UIImage(named: "icon_pencil"), for: .normal)
        return view
    }()
    
    lazy var eraserBtn: UIButton = {
        let view = UIButton()
        view.setImage(UIImage(named: "icon_eraser"), for: .normal)
        return view
    }()
    
    lazy var undoBtn: UIButton = {
        let view = UIButton()
        view.setImage(UIImage(named: "icon_undo"), for: .normal)
        return view
    }()
    
    lazy var redoBtn: UIButton = {
        let view = UIButton()
        view.setImage(UIImage(named: "icon_redo"), for: .normal)
        return view
    }()
    
    lazy var speedView: SpeedView = {
        let view = SpeedView()
        view.delegate = self
        return view
    }()
    
    var displayLink: CADisplayLink?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.view.backgroundColor = UIColor(hex: "#222222")
        view.addSubview(topBar)
        topBar.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.height.equalTo(44 + ScreenStatusBarHeight)
            make.top.equalToSuperview()
        }
        
        topBar.addSubview(backBtn)
        backBtn.snp.makeConstraints { make in
            make.width.height.equalTo(18)
            make.bottom.equalToSuperview().offset(-13)
            make.left.equalToSuperview().offset(20)
        }
        
        topBar.addSubview(musicBtn)
        musicBtn.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.centerY.equalTo(backBtn)
        }
        
        topBar.addSubview(viewBtn)
        viewBtn.snp.makeConstraints { make in
            make.height.equalTo(30)
            make.width.equalTo(100)
            make.right.equalToSuperview().offset(-20)
            make.centerY.equalTo(backBtn)
        }
        
        view.addSubview(bgView)
        bgView.snp.makeConstraints { make in
            make.top.equalTo(topBar.snp.bottom)
            make.left.right.bottom.equalToSuperview()
        }
        
        view.addSubview(canvasView)
        let w = min(ScreenHeight, ScreenWidth) - 70 - 100 - ScreenStatusBarHeight
        canvasView.snp.makeConstraints { make in
            make.bottom.equalToSuperview().offset(-100)
            make.width.height.equalTo(w)
            make.centerX.equalToSuperview()
        }
        
        view.addSubview(toolView)
        toolView.snp.makeConstraints { make in
            make.width.equalTo(46)
            make.height.equalTo(308)
            make.left.equalToSuperview().offset(20)
            make.centerY.equalTo(canvasView)
        }
        
        toolView.addSubview(colorBtn)
        colorBtn.snp.makeConstraints { make in
            make.width.height.equalTo(20)
            make.top.equalTo(24)
            make.centerX.equalToSuperview()
        }
        
        toolView.addSubview(penBtn)
        penBtn.snp.makeConstraints { make in
            make.width.height.equalTo(24)
            make.top.equalTo(colorBtn.snp.bottom).offset(30)
            make.centerX.equalToSuperview()
        }
        
        toolView.addSubview(eraserBtn)
        eraserBtn.snp.makeConstraints { make in
            make.width.height.equalTo(24)
            make.top.equalTo(penBtn.snp.bottom).offset(28)
            make.centerX.equalToSuperview()
        }
        
        toolView.addSubview(redoBtn)
        redoBtn.snp.makeConstraints { make in
            make.width.height.equalTo(24)
            make.bottom.equalTo(-24)
            make.centerX.equalToSuperview()
        }
        
        toolView.addSubview(undoBtn)
        undoBtn.snp.makeConstraints { make in
            make.width.height.equalTo(24)
            make.bottom.equalTo(redoBtn.snp.top).offset(-28)
            make.centerX.equalToSuperview()
        }
        
        view.addSubview(speedView)
        speedView.snp.makeConstraints { make in
            make.width.equalTo(300)
            make.height.equalTo(50)
            make.bottom.equalToSuperview().offset(-23)
            make.centerX.equalToSuperview()
        }
        
        musicBtn.setTitle("冥想的力量", for: .normal)
    }
    
    
    @objc func actionBack() {
        
    }
}

extension EditViewController: SpeedViewDelegate {
    func speedViewValueChanged(_ speed: CGFloat) {
        canvasView.speed = speed
    }
}
